﻿using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Reflection;
using AnyOffice.BIZ;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using FredCK.FCKeditorV2;
using log4net;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.notify.manage
{
    public partial class _tnew : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

      
        protected void btnAddFile_Click(object sender, EventArgs e)
        {
            if (this.fileAttach.PostedFile.ContentLength > 0)
            {
                try
                {
                    string uniqueTempPath = this.ViewState["UniqueTempPath"].ToString();
                    if (uniqueTempPath == "")
                    {
                        uniqueTempPath = AttachmentBiz.GetUniqueTempPath();
                        this.ViewState["UniqueTempPath"] = uniqueTempPath;
                    }
                    Upload upload = new Upload();
                    upload.AutoCreateDirectory = true;
                    upload.OverwriteFile = true;
                    upload.DestinationPath = uniqueTempPath;
                    ConfigManager manager = new ConfigManager();
                    upload.MaxSize = manager.AttachMaxSize;
                    upload.GetFile(this.fileAttach.PostedFile);
                    upload.DoUpload();
                    this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) + upload.FileSize;
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    this.ViewState["FileList"] = list;
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) + 1;
                    this.ltbFileList.Items.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) != 0)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) > 0)
                    {
                        this.pnlAttach.Visible = true;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
            }
        }
        protected void btnRemoveFile_Click(object sender, EventArgs e)
        {
            if (this.ltbFileList.Items.Count != 0)
            {
                try
                {
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Remove(this.ltbFileList.SelectedItem.Text);
                    this.ViewState["FileList"] = list;
                    string fileName = this.ViewState["UniqueTempPath"].ToString() + this.ltbFileList.SelectedItem.Text;
                    FileInfo info = new FileInfo(fileName);
                    if (info.Exists)
                    {
                        this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) - info.Length;
                        if (File.Exists(fileName))
                        {
                            File.Delete(fileName);
                        }
                    }
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) - 1;
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) >= 1)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    this.ltbFileList.Items.Remove(this.ltbFileList.SelectedItem.Text);
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) == 0)
                    {
                        if (Directory.Exists(this.ViewState["UniqueTempPath"].ToString()))
                        {
                            Directory.Delete(this.ViewState["UniqueTempPath"].ToString());
                        }
                        this.ViewState["UniqueTempPath"] = "";
                        this.pnlAttach.Visible = false;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string attachmentId = "";
            try
            {
                if (this.ViewState["UniqueTempPath"].ToString() != "")
                {
                    string uniquePath = AttachmentBiz.GetUniquePath(out attachmentId);
                    Directory.Move(this.ViewState["UniqueTempPath"].ToString(), uniquePath);
                    AttachmentData data = new AttachmentData();
                    AttachmentDao dao = new AttachmentDao();
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    for (int i = 0; i < list.Count; i++)
                    {
                        FileInfo info = new FileInfo(uniquePath + list[i].ToString());
                        if (info.Exists)
                        {
                            data.Type = 3;
                            data.AttachmentId = attachmentId;
                            data.Name = info.Name;
                            data.AddTime = DateTime.Now;
                            data.TotalSize = (int)info.Length;
                            data.UserId = base.User.Identity.Name;
                            dao.Insert(base.Conn, data);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception);
                }
            }
            NotifyData data2 = new NotifyData();
            NotifyDao dao2 = new NotifyDao();
            if (attachmentId != string.Empty)
            {
                data2.AttachmentId = attachmentId;
            }
            if (this.ToId.Value != string.Empty)
            {
                data2.ToId = this.ToId.Value;
            }
            if (this.ToPriv.Value != string.Empty)
            {
                data2.ToPriv = this.ToPriv.Value;
            }
            if (this.ToUser.Value != string.Empty)
            {
                data2.ToUser = this.ToUser.Value;
            }
            data2.Subject = this.txtSubject.Text;
            data2.TypeId = Convert.ToInt32(this.ddlTypeId.SelectedValue);
            if (this.txtBeginDate.Value != "")
            {
                data2.BeginDate = DateTime.Parse(this.txtBeginDate.Value);
            }
            if (this.txtEndDate.Value != "")
            {
                data2.EndDate = DateTime.Parse(this.txtEndDate.Value);
            }
            data2.Important = this.ckbImportant.Checked;
            data2.Content = this.txtContent.Value;
            data2.FromId = base.User.Identity.Name;
            data2.SendTime = DateTime.Now;
            int num2 = dao2.Insert(base.Conn, data2);
            if (this.ckbSms.Checked || this.ckbMobile.Checked)
            {
                SmsData data3 = new SmsData();
                SmsDao dao3 = new SmsDao();
                data3.Type = 1;
                data3.RelateId = num2;
                data3.FromId = base.User.Identity.Name;
                if (this.ToId.Value != string.Empty)
                {
                    data3.ToDeptList = this.ToId.Value;
                }
                if (this.ToPriv.Value != string.Empty)
                {
                    data3.ToPrivList = this.ToPriv.Value;
                }
                if (this.ToUser.Value != string.Empty)
                {
                    data3.ToIdList = this.ToUser.Value;
                }
                data3.Content = "请查看配件通知！\n标题：" + this.txtSubject.Text;
                data3.RemindFlag = true;
                data3.SendTime = DateTime.Now;
                dao3.BizSend(base.Conn, data3, this.ckbSms.Checked, this.ckbMobile.Checked);
            }
            this.pnlMain.Visible = false;
            this.litMessage.Text = base.ShowPrompt("配件通知发布成功！", "index1.aspx");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                DataTable table = new NotifyTypeDao().SelectAll(base.Conn);
                if (table.Rows.Count > 0)
                {
                    this.ddlTypeId.DataSource = table;
                    this.ddlTypeId.DataBind();
                    this.ddlTypeId.Items.Insert(0, new ListItem("请选择", ""));
                }
                else
                {
                    this.ddlTypeId.Items.Insert(0, new ListItem("未定义分类", ""));
                }
                this.ViewState["FileNums"] = 0;
                this.ViewState["FileList"] = new ArrayList();
                this.ViewState["UniqueTempPath"] = "";
                this.ViewState["TotalSize"] = 0;
                this.btnAddFile.Attributes.Add("onclick", "return CheckFile();");
                this.btnSubmit.Attributes.Add("onclick", "return CheckForm();");
                this.ckbMobile.Visible = SmsDao.IsMobile(SmsDao.SmsType.Notify);
            }
        }

    }
}
